<html>
<head><meta charset="utf-8"><title>Proc macros in regular crates · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html">Proc macros in regular crates</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198557144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198557144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198557144">(May 23 2020 at 23:41)</a>:</h4>
<p>Would it ever be possible to have a proc Macro <em>definition</em> inside of a regular crate? I keep running into the case where I want to have a compile-time and run-time version of an API, which share virtually all the same internal code with different interfaces, and currently to achieve that you have to split your single crate into three-four crates, which becomes much more of a hassle to maintain and publish on <a href="http://crates.io">crates.io</a>.</p>
<p>I understand why it wouldn’t be possible to both define a proc macro and use it in the same crate, but I think allowing just the definition would still be a huge quality of life improvement for me.</p>



<a name="198571687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198571687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198571687">(May 24 2020 at 07:51)</a>:</h4>
<p>Seems to me like the problems of "compiling multiple different crates from one set of source files" remain the same when when the crate doesnt use its "own" proc_macro.</p>



<a name="198571688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198571688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198571688">(May 24 2020 at 07:51)</a>:</h4>
<p>and AFAIK that is what makes mixing proc_macro and lib code so hard.</p>



<a name="198578249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198578249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198578249">(May 24 2020 at 11:06)</a>:</h4>
<p>That's true, and to be honest I don't even want a proc macro. I just want to be able to perform certain operations at compile time, such as reading and parsing files from a directory, and currently proc macros are the only way I know to do that.</p>



<a name="198578373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198578373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198578373">(May 24 2020 at 11:11)</a>:</h4>
<p>I might not understand the problem fully but what about <code>build.rs</code> file?</p>



<a name="198582321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198582321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198582321">(May 24 2020 at 12:59)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> I want to expose a compile time API to users, a <code>build.rs</code> only works inside my crate. To pick a simple example I want to have <code>std::env::var</code> and <code>env!</code>.</p>



<a name="198692358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198692358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198692358">(May 25 2020 at 18:25)</a>:</h4>
<p>It wouldn't work for that particular case, but some such things could be done with <code>const fn</code>. For those that can't, though, a proc macro seems right, and it would be nice to be able to provide one from the same crate.</p>



<a name="198706745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198706745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198706745">(May 25 2020 at 22:44)</a>:</h4>
<p>Thinking on this more I was thinking my problem is more with Cargo than rustc and having to turn my project into a cargo workspace just to support macros, I was thinking what if instead of having to set <code>proc-macro = true</code>, cargo had a macro target similar to bin, that could act as the place to declare the macros that are exported as part of your library. E.g.</p>
<div class="codehilite"><pre><span></span><code><span class="k">[[macros]]</span>
<span class="n">name</span> <span class="o">=</span> <span class="err">“</span><span class="n">derive</span><span class="err">”</span>
<span class="n">path</span> <span class="o">=</span> <span class="err">“</span><span class="n">macros</span><span class="err">/</span><span class="n">derive</span><span class="p">.</span><span class="n">rs</span><span class="err">”</span>
</code></pre></div>



<a name="198706763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198706763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198706763">(May 25 2020 at 22:45)</a>:</h4>
<p>(deleted)</p>



<a name="198708208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198708208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198708208">(May 25 2020 at 23:20)</a>:</h4>
<p>This could also provide the benefit of being usable in your crate if it doesn’t depend on it. So it’s easier to have macros as a private implementation detail, which I have wanted more recently.</p>
<p>Either way I think I’ll write a post about it on internals since it’s bigger in scope.</p>



<a name="198711500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Proc%20macros%20in%20regular%20crates/near/198711500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Proc.20macros.20in.20regular.20crates.html#198711500">(May 26 2020 at 00:42)</a>:</h4>
<p>Internals Thread <a href="https://internals.rust-lang.org/t/pre-rfc-add-macros-target-to-cargo-manifest/12428">https://internals.rust-lang.org/t/pre-rfc-add-macros-target-to-cargo-manifest/12428</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>